import Common from '../common'
var Js = Common('product')
export default {
	name: 'product',
	mixins: [Js],
	components: {},
	data() {
		return {
			fields: [{
					key: 'id',
					label: 'ID',
					application: [],
					componentName: 'sls-input',
					listType: '',
					width: 50
				},
				{
					key: 'product_no',
					label: '商品NO',
					application: [],
					componentName: 'sls-input',
					listType: '',
					width: 100
				},
				{
					key: 'title',
					label: '商品名称',
					application: ['编辑', '添加'],
					componentName: 'sls-input',
					listType: 'normal',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入商品名称',
						clearable: true,
						defaultValue: '',
						optionsName: '',
						changeFunc: function(val, self) {
							if (val) {
								self.searchItem.title = ['LIKE',['%'+val+'%']];
							} else {
								delete self.searchItem.title;
							};

						},
					},
				},
				{
					key: 'price',
					label: '售价',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:100
				},
				{
					key: 'o_price',
					label: '成本价',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:100
				},
				{
					key: 'tj_type',
					label: '是否畅销',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width:100,
          formatter:function(val){
            return val.tj_type==1?'是':'否'
          }
				},
				{
					key: 'allStock',
					label: '库存',
					application: ['编辑', '添加'],
					componentName: 'sls-input',
					listType: 'normal',
					width:100,
				},
				{
					key: 'sale_count',
					label: '销量',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 100
				},
				{
					key: 'category_id',
					label: '商品类别',
					application: ['编辑', '添加'],
					formatter: function(val, tests) {
						return val.labelTit
					},
					componentName: 'sls-select',
					optionsName: 'labelOptions',
					listType: 'normal',
					limit: 10,
					defaultProps: {
						label: 'title',
						value: 'id',
					},
					placeholder: '请选择类别',
					width: 150,
					header_search: {
						componentName: 'sls-select',
						optionsName: 'labelOptions',
						placeholder: '请选择类别',
						multiple: true,
						defaultValue: [],
						changeFunc: function(val, self) {
							if (val && val.length > 0) {
								self.FIND_IN_SET = [];
								self.FIND_IN_SET.push({
									type: 'or',
									arr: val,
									key: 'category_id'
								});
							} else {
								self.FIND_IN_SET = [];
							};
						},
					}
				},
				{
					key: 'content',
					label: '内容',
					application: ['编辑', '添加'],
					componentName: 'tinymce-editor',
					listType: '',
				},
				{
					key: 'listorder',
					label: '排序',
					application: ['编辑', '添加'],
					componentName: 'sls-input',
					listType: 'normal',
					width: 100
				},

				{
					key: 'mainImg',
					label: '展示图',
					customSlot: 'mainImg',
					application: ['编辑', '添加'],
					componentName: 'upload',
					listType: 'normal',
					limit: 10,
					dialogStyle: 'width:90%',
					width: 100
				},
				{
					key: 'bannerImg',
					label: '详情图',
					customSlot: 'bannerImg',
					application: ['编辑', '添加'],
					componentName: 'upload',
					listType: '',
					limit: 10,
					dialogStyle: 'width:90%',
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};
						},
					},
					width: 200,
				},
				{
					label: '操作',
					listType: 'deal',
					width: 110
				},
			],
			// 按钮配置
			btn_info: [{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {
						apiName: function(self) {
							return "api_productUpdate"
						},
						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],
								},
								data: {
									status: -1
								}
							};
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '添加商品'
					},
					funcType: 'func',
					func: {
						func: function(self) {
							self.$router.push({
								path: '/home/productdetail',
								name: '平台商品详情',
								params: {
									type: 1,
									delKeepAlive: true,
                  hasSku: true,
                  categorySearchItem: {
                    type:3
                  },
									title: self.originFormData.title,
									backPath: '/home/product'
								}
							});
						},
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '商品详情'
					},
					funcType: 'func',
					func: {
						func: function(self) {
							self.$router.push({
								path: '/home/productdetail',
								name: '平台商品详情',
								params: {
									product_id: self.originFormData.id,
									type: 1,
									delKeepAlive: true,
									hasSku: true,
									categorySearchItem: {
                    type:3
                  },
									title: self.originFormData.title,
                  backPath: '/home/product'
								}
							});
						},
					},
				}
			],

			searchItem: {
				type: 1,
			},
			startSearchItem: {
				type: 1,
			},
			optionData: {
				labelOptions: [],
				skuOptions: [],
				statusOptions: [{
					text: '启用',
					value: 1
				}, {
					text: '禁用',
					value: -1
				}],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			},

		}

	},

	mounted() {
		const self = this;
		self.table_arguments.tree_props = {
			children: 'child',
			child: 'child',
			hasChildren: 'child'
		}
		// if(this.$store.state.user.userinfo.role!=3){
		// 	self.searchItem.shop_no = this.$store.state.user.userinfo.user_no;
		// 	self.startSearchItem.shop_no = this.$store.state.user.userinfo.user_no;
		// }
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData();
			this.initLabelData();
		},

		async initLabelData(isNew) {

			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				type: 3
			};
			postData.order = {
				listorder: 'asc'
			};
			var res = await self.$$api_labelGet({
				data: postData
			});
			self.optionData.labelOptions = res.info.data;

		},


		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			self.table_arguments.loading = true;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			if (self.FIND_IN_SET.length>0) {
				postData.FIND_IN_SET = self.$$cloneForm(self.FIND_IN_SET);
			};
			postData.getAfter = {
				sku:{
					tableName:'Sku',
					middleKey:'id',
					key:'product_id',
					condition:'=',
				},
        label:{
        	tableName:'Label',
        	middleKey:'category_id',
        	key:'id',
        	condition:'FIND_IN_SET',
          type: 'or',
          order: {
            id: 'asc'
          }
        }
			}
			var res = await self.$$api_productGet({
				data: postData
			});
			for (var i = 0; i < res.info.data.length; i++) {
				res.info.data[i]['labelTit'] = [];
				var data = res.info.data[i]['label'];
				for (let key in data) {
					res.info.data[i]['labelTit'].push(data[key]['title']);
				}
				res.info.data[i]['labelTit'] = res.info.data[i]['labelTit'].join('/');
        res.info.data[i].allStock = 0;
        res.info.data[i].sku.length>0&&res.info.data[i].sku.forEach((item,index)=>{
          res.info.data[i].allStock += item.stock;
        })
			}
			self.mainData = res.info.data;
			self.paginate.count = res.info.total;
			self.table_arguments.loading = false;

		}

	},


}
